/*
 * 题库
 */

import { createAPI } from '@/utils/request'
import moment from 'moment'
// 基础题库列表
const difficultyMapping = new Map([
  ['1', '简单'],
  ['2', '一般'],
  ['3', '困难']
])
export const questionslist = data => createAPI('/questions', 'get', data)
  .then(res => {
    return {
      items: res.data.items.map(d => ({
        ...d,
        questionType: d.questionType === '2' ? '多选' : '单选',
        difficulty: difficultyMapping.get(d.difficulty),
        addDate: moment(d.addDate).format('YYYY-MM-DD, hh:mm:ss'),
      })),
      total: res.data.counts,
    }
  })
  .catch(() => ({}))

// 基础题库详情
export const detailquestion = data => createAPI(`/questions/${data.id}`, 'get', data)
  .then(res => res.data)
  .catch(() => ({}))

// 批量导入题
export const batch = data => createAPI('/questions/batch', 'post', data)

// 基础题库添加
export const addquestions = data => createAPI('/questions', 'post', data)

// 基础题库修改
export const updateQuestions = data => createAPI(`/questions/${data.id}`, 'put', data)

// 基础题库删除
export const removequestions = id => createAPI(`/questions/${id}`, 'delete', id)

// 精选题库加入取消
export const choiceAdd = data =>createAPI(`/questions/choice/${data.id}/${data.choiceState}`, 'patch', data)

// 精选题库列表
const choiceListMaping = new Map([
  [0, '待审核'],
  [1, '已审核'],
  [2, '已拒绝']
])
export const choiceList = data => createAPI('/questions/choice', 'get', data)
  .then(res => {
    return {
      items: res.data.items.map(d => ({
        ...d,
        questionType: d.questionType === '2' ? '多选' : '单选',
        difficulty: difficultyMapping.get(d.difficulty),
        addDate: moment(d.addDate).format('YYYY-MM-DD, hh:mm:ss'),
        chkState: choiceListMaping.get(d.chkState),
        // publishState: d.publishState === 1 ? '已发布' : '待发布',
        announceding: d.chkState === 1 ? d.publishState === 1 ? '已发布' : '待发布' : '待审核',
        audit: d.creator
      })),
      total: res.data.counts,
    }
  })
  .catch(() => ({}))

// 精选题库上下架
export const choicePublish = data =>createAPI(`/questions/choice/${data.id}/${data.publishState}`, 'post', data)

// 试题审核
export const choiceCheck = data =>createAPI(`/questions/check/${data.id}`, 'post', data)

// 试题审核意见列表
export const auditOpinions = data =>
  createAPI(`/questions/${data.id}/auditOpinions`, 'get', data)

// 出题记录
export const setRecords = data =>
  createAPI(`/questions/${data.id}/setRecords`, 'get', data)

// 组题列表
export const randoms = data => createAPI('/questions/randoms', 'get', data)

// 组题列表删除
export const removeRandoms = data =>
  createAPI(`/questions/randoms/${data.id}`, 'delete', data)
